package com.gitee.magic.converter;

import com.gitee.magic.core.converter.AbstractConverterEditor;

/**
 * @author start
 */
public class EnumConverterEditor extends AbstractConverterEditor<String>  {
	
	public EnumConverterEditor(Class<?> prototype) {
		super(prototype);
	}

	@Override
	public void restore(Object value) {
		if(value!=null) {
			for(Object e:getPrototype().getEnumConstants()) {
				if(String.valueOf(e).equals(String.valueOf(value))) {
					setValue(e);
					return;
				}
			}
		}
	}

	@Override
	public String converter() {
		if(getValue()==null) {
			return null;
		}
		return String.valueOf(((Enum<?>)getValue()).name());
	}
	
}
